﻿using System;
using System.Collections.Generic;
using KC.Framework.Util;
using KC.Framework.Tenant;
using KC.Model.Storage.Queue;
using KC.Service.Core.Events;
using KC.DTO.Account;
using KC.Service.Storage;

namespace KC.Service.Account.EventHandlers
{
    public class UserCreatedHandler : IHandler
    {
        private UserDTO _user;
        private string _pwd;
        private Tenant _tenant;

        public UserCreatedHandler(Tenant tenant, UserDTO user, string password)
        {
            _user = user;
            _pwd = password;
            _tenant = tenant;
        }

        public bool Handle()
        {
            LogUtil.LogInfo("------------UserCreatedHandler---------");

            try
            {
                //var emailBody = "您好，{0}！<br/>    您在公司（{1}）内部系统的登录用户已经创建成功，登录初始密码为: {2}, 请登录系统后马上更改密码。<br/>如遇问题请您与公司系统管理员联系。";
                var emailBody =
                    @"<!doctype html><html lang='en'><head><meta charset='UTF-8'><title></title><style>*,html,body{{margin:0;padding:0}}.divdiv{{width:638px;position:relative;margin:0 auto;background:#17212E;padding:20px;padding-top:0}}table{{width:100%;background:#454d5b;text-align:center;margin:0 auto;margin-top:50px;margin-bottom:50px;border-collapse:collapse}}.p_white{{color:#fff}}table tr td:first-child{{text-align:left;width:33%}}table tr td{{text-align:left;padding:9px;color:#fff;min-width:136px}}table tr th{{color:#fff;padding:13px;background:#4b5761;text-align:left}}table tr td p{{word-break:break-all;color:inherit}}.app_tb tr td:first-child{{text-align:left}}tr{{border:1px solid #636363}}.tips tr{{border:none}}.tips tr td:first-child{{text-align:left}}a{{text-decoration:none;color:#fff;word-break:break-all}}.guanggao_contain{{width:100%;position:relative;float:left;margin-top:30px;margin-bottom:30px}}.guanggao_logo{{float:left;margin-left:20px;display:block;position:relative;background-position:35px 14px;color:#afafaf}}.guanggao_logo_p{{float:left;margin-left:20px;display:block;position:relative;background-position:35px 14px;color:#afafaf;margin-bottom:10px;clear:both;width:100%}}.tips{{min-height:50px;background:0 0;font-size:12px}}.guanggao_logo:hover{{color:#fff}}.title p{{ font-size: 16px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif; font-weight: bold;margin: 8px;
}}</style></head><body><div class='divdiv'><div class='guanggao_contain'><a href='http://www.cfwin.com/' target='_blank' class='guanggao_logo'>
<img src=''></a></div>
<div class='title'><p>主题：【产融协作员工账号创建成功-财富共赢】</p><p>{0}员工{1},您好！</p><p>您公司的系统管理员为您添加的产融协作员工账号已创建成功。</p> </div>
<table><tr><td><p>公司名称:</p></td><td><p>{0}</p></td></tr>
<tr><td><p>租户代码:</p></td><td><p>{2}</p></td></tr>
<tr><td><p>账号：</p></td><td><p>{3}</p></td></tr>
<tr><td><p>初始密码：</p></td><td><p>{4}</p></td></tr>
</table>
<div style='padding-top:12px' colspan='2'><span style='font-size: 17px; color: #c6d4df; font-family: Arial, Helvetica, sans-serif; font-weight: bold;'><p>登陆地址：<a href='http://sso.cfwin.com' target='_blank'>http://sso.cfwin.com</a>。请登录后立即修改密码，如遇问题，请与公司系统管理员联系。</p></span></div><div class='clear'></div><div class='tips'><p class='guanggao_logo_p'>友情链接</p>
<a href='http://www.cfwin.com/' target='_blank' class='guanggao_logo'>财富共赢金融超市</a> <a href='http://www.starlu.com/' target='_blank' class='guanggao_logo'>大陆之星赊销商城</a> <a href='http://www.cfwinclub.com/' target='_blank' class='guanggao_logo'>财富共赢俱乐部</a></div></div></body></html>";
                var mail = new EmailInfo
                {
                    Tenant = _tenant.TenantName,
                    UserId = _user.UserId,
                    EmailTitle = "创建登陆用户成功",
                    EmailBody = string.Format(emailBody,_tenant.TenantDisplayName, _user.DisplayName, _tenant.TenantName,_user.UserName, _pwd),
                    SendTo = new List<string>() { _user.Email }
                };
                new TenantStorageQueueService(_tenant).InsertEmailQueue(mail);

                return true;
            }
            catch (Exception ex)
            {
                LogUtil.LogError("创建用户成功后，处理后续业务出错：", ex.Message + Environment.NewLine + ex.StackTrace);
                return false;
            }
        }
    }


}
